 
    /** 
     * 文件名：Test.java 
     * 
     * 版本信息： 
     * 日期：2015年1月16日 
     * Copyright 足下 Corporation 2015 
     * 版权所有 
     * 
     */ 
    
package com.qyer.util.log.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.qyer.util.log.entity.TrackLog;
import com.qyer.util.log.service.LogService;
import com.qyer.util.log.service.impl.LogService4NginxImpl;
import com.qyer.util.log.util.HiveFormatUtil;
import com.qyer.util.log.util.LogFatory;


public class CopyOfTestTrack  {
		public static String getMatcher(String regex, String source) {  
	        String result = "";  
	        Pattern pattern = Pattern.compile(regex);  
	        Matcher matcher = pattern.matcher(source);  
	        while (matcher.find()) {  
	            result = matcher.group(1);//只取第一组  
	        }  
	        return result;  
	    } 
		
		private static List<String> listUGC = new ArrayList<String>();// 存储ugc正则
		static HashMap<String, String> cityNameMaps = new HashMap<String, String>();
		static HashMap<String, String> countryNameMaps = new HashMap<String, String>();

		public static void main(String[] args) {
			getCityNameMaps();
			getUgcType();
			Map<String , String>cateMap = getCateMap();
			
			LogService lli = (LogService)LogFatory.getInstance(LogService4NginxImpl.class.getName());
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/115.182.69.217_log.1453306205117";
			
			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                // 显示行号
	                //System.out.println("line " + line + ": " + tempString);
	                line++;
	                
	                TrackLog ll =lli.getTrackFormat(tempString);
	                
	                if(ll!=null){
	                	 if (ll.getGuid()!=null && !"".equals(ll.getGuid())&&ll.getTrackTime()!=null&&!"".equals(ll.getTrackTime())){
	                		ll =lli.setUGC(ll, listUGC, cityNameMaps, countryNameMaps, cateMap, null,null);
	   	 	                 
	                		StringBuilder  outStr = new StringBuilder ("");
	             			
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getUserId())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getGuid())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getTrackTime())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getSession_timestamp())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getTrackTime())).append(Constants.FS);
//	             			outStr.append("+08:00").append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getUserIp())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getLon())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getLat())).append(Constants.FS);
	             			outStr.append(HiveFormatUtil.getFeild(ll.getCurUrl())).append("\t");
	             			outStr.append(HiveFormatUtil.getFeild(ll.getRefUrl())).append("\t");
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getType())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getCategory())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getRa_arg())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getUserAgent())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getChannel1())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getPageType())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getPlatform())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getUgc_content())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getMapFeild(ll.getExtParamMap())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getRefer_ugc_type())).append(Constants.FS);
//	             			outStr.append(HiveFormatUtil.getFeild(ll.getRefer_ugc_content()));
	             			System.out.println(ll.getUrlParamMap());
	             			System.out.println(ll.getExtParamMap());
	             			System.out.println(ll.getCurlParamMap());
	             			
	             			System.out.println("----------------------");
	             			
//	             			if(ll.getCurUrl().equals("http://www.qyer.com/")){
//	             				
//	             				System.out.println(ll.getUgc_type()+"+++"+ll.getRefer_ugc_type()+"+++"+ll.getPath() );
//	             			}
	                	 }
	                }	 
	               
	            }
	            long s2= System.currentTimeMillis(); 
	            System.out.println("a=="+a);
	            System.out.println("i=="+i);
	            System.out.println("time ===="+ (s2-s1)/1000);
	            reader.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
			
			
		}
		
		private static Map<String, String> getChannelMaps() {
			Map<String , String > channelMaps = new HashMap<String,String>();
			
			//一级频道
			channelMaps.put("plan", "plan");
			channelMaps.put("z", "z");
			channelMaps.put("search", "search");
			channelMaps.put("bbs", "bbs");
			channelMaps.put("ask", "ask");
			channelMaps.put("guide", "guide");
			channelMaps.put("place", "place");
			channelMaps.put("guide", "guide");
			//二级频道
			//place
			channelMaps.put("edit", "edit");
			
			//折扣 z 二级
			channelMaps.put("flights", "flights");
			channelMaps.put("hotels", "hotels");
			channelMaps.put("free", "free");
			channelMaps.put("cruise", "cruise");
			channelMaps.put("car", "car");
			channelMaps.put("safe", "safe");
			channelMaps.put("qz", "qz");
			channelMaps.put("local", "local");
			channelMaps.put("ot", "ot");
			channelMaps.put("other", "other");
			
			return channelMaps;
		}

	
		
		public static Map<String , String > getCityMaps(){
			Map<String , String > cityMap = new HashMap<String,String>();
			
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/clean_track/city_country_list.csv";

			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                line++;
	                String[] array = tempString.split("#\\|~");
					if (array.length != 6) {
						System.out.println(line + array.length);
						continue;
					}
					String city;
					if (array[2].length() > 0 && !array[2].equals("None")) {
						city = array[2];
					} else {
						city = array[1];
					}
					String cityID = array[0];
					String country = array[4];
					String countryID = array[3];

					cityMap.put(city, cityID);
	            }
	            reader.close();
	            return cityMap;
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null; 
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
		}
		
		public static Map<String , String > getCountryMaps(){
			Map<String , String > cityMap = new HashMap<String,String>();
			
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/clean_track/city_country_list.csv";

			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader	= new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	                line++;
	                String[] array = tempString.split("#\\|~");
					if (array.length != 6) {
						System.out.println(line + array.length);
						continue;
					}
					String city;
					if (array[2].length() > 0 && !array[2].equals("None")) {
						city = array[2];
					} else {
						city = array[1];
					}
					String cityID = array[0];
					String country = array[4];
					String countryID = array[3];

					cityMap.put(country, countryID);
	            }
	            reader.close();
	            return cityMap;
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null; 
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
		}
		
		
		
		
		
		public static Map<String , String > getCityNameMaps(){
			Map<String , String > cityMap = new HashMap<String,String>();
			
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/clean_track/city_country_list.csv";

			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	            	line++;
	                String[] array = tempString.split("#\\|~");
					if (array.length != 6) {
						System.out.println(line + array.length);
						continue;
					}
					String cityStr;
					if (array[2].length() > 0 && !array[2].equals("None")) {
						cityStr = array[2];
					} else {
						cityStr = array[1];
					}
					String cityID = array[0];
					String countryStr = array[4];
					String countryID = array[3];

					cityNameMaps.put(cityStr, cityID);
					countryNameMaps.put(countryStr, countryID);
	            }
	            reader.close();
	            return cityMap;
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null; 
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
		}
		
		public static Map<String , String > getCateMap(){
			Map<String , String > cityMap = new HashMap<String,String>();
			
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/clean_track/catemap.txt";

			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	            	line++;
	            	String []strArr = tempString.split("\t", -1);
					
					if(strArr.length<4){
						continue ;
					}
					
					cityMap.put(strArr[1]+","+strArr[2], strArr[3]);
	            }
	            reader.close();
	            return cityMap;
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null; 
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
		}
		
		
		public static Map<String , String > getUgcType(){
			
			String fileName="/Users/sunhongxi/Documents/sshfileupdown/clean_track/ugctype.txt";

			File file = new File(fileName);
	        BufferedReader reader = null;
	        try {
	            System.out.println("以行为单位读取文件内容，一次读一整行：");
	            reader = new BufferedReader(new FileReader(file));
	            String tempString = null;
	            int line = 1;
	            int i=0 , a =0 ; 
	            
	            long s1= System.currentTimeMillis(); 
	            // 一次读入一行，直到读入null为文件结束
	            while ((tempString = reader.readLine()) != null) {
	            	String[] array = tempString.split("@@");
					if (array.length > 0) {
						if (!listUGC.contains(array[0])) {
							listUGC.add(array[0]);
						}

					}
	            }
	            reader.close();
	            return null; 
	        } catch (IOException e) {
	            e.printStackTrace();
	            return null; 
	        } finally {
	            if (reader != null) {
	                try {
	                    reader.close();
	                } catch (IOException e1) {
	                }
	            }
	        }
		}
		
		
		
}
